//
//  DestinationTableViewController.m
//  mafengwo
//
//  Created by Mac on 16/3/13.
//  Copyright © 2016年 Mac. All rights reserved.
//

#import "DestinationTableViewController.h"
#import "PhotosViewController.h"
#import "TravelDirectSeedingVC.h"

@interface DestinationTableViewController (){

    UIRefreshControl *re;//下拉刷新控件
    UIView *_searchView;//搜索框视图
    //搜索框
    UITextField *textF;
    CGFloat _offsetY;//tableview的y偏移量，用于头视图的移动

    //附近地图的滚动label
    IBOutlet UIView *scrollADView;
    //上面的滚动视图
    UIScrollView *_scroV;
    NSInteger currentPage;

    //旅行直播
    IBOutlet UIImageView *oneImgV;
    IBOutlet UIImageView *oneLogo;
    IBOutlet UIImageView *twoImgV;
    IBOutlet UIImageView *twoLogo;
    IBOutlet UIImageView *threeImgV;
    IBOutlet UIImageView *threeLogo;
    //自由行特惠
    IBOutlet UIImageView *freeImg1;
    IBOutlet UILabel *freeLabel;
    IBOutlet UILabel *freePrice1;
    IBOutlet UIButton *freeDiscount1;
    IBOutlet UIImageView *freeImg2;
    IBOutlet UILabel *freeLabel2;
    IBOutlet UILabel *freePrice2;
    IBOutlet UIButton *freeDiscount2;
    IBOutlet UIImageView *freeImg3;
    IBOutlet UILabel *freeLabel3;
    IBOutlet UILabel *freePrice3;
    IBOutlet UIButton *freeDiscount3;

    IBOutlet UIButton *freeSeeMore;

    //查看382个景点

    IBOutlet UIButton *lookD;
    //查看更多美食
    IBOutlet UIButton *delicious;
    //地图上查看全部
    IBOutlet UIButton *lookAllOnMap;
}

@end

@implementation DestinationTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];


    //1.寻找XIb
    NSArray* array =  [[NSBundle mainBundle]loadNibNamed:@"ScrollView" owner:nil options:nil];

    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 414, 225)];
    scrollView.contentSize = CGSizeMake(414*2, 225);


    //3.隐藏滑动条
    //（1）水平滑动条的显示
    scrollView.showsHorizontalScrollIndicator = NO;
    // (2)设置垂直滑动条的显示
//    scrollView.showsVerticalScrollIndicator = YES;

    //4.设置分页效果(默认为NO)
    scrollView.pagingEnabled = YES;
    [_scrollView addSubview:scrollView];

    UIView *view1 = [array lastObject];
    UIView *view2 = [array firstObject];
    view2.frame = CGRectMake(414, 0, 414, 225);

    [scrollView addSubview:view2];
    [scrollView addSubview:view1];


    //1. 创建一个分页控制器
    UIPageControl* pageC =  [[UIPageControl alloc]initWithFrame:CGRectMake(65, 630, 390, 50)];
    [self.view addSubview:pageC];

    //2. 设置总页数
    pageC.numberOfPages = 3;


    //---------下拉刷新----------
    //IOS 6之后，可以为tableView添加一个UIRefreshControl(下啦刷新).关于下啦刷新的布局问题不必要去考虑，系统会自动将他放到表视图里,给他设置一个位置

    //1.创建下拉刷新控件
    re = [[UIRefreshControl alloc]init];
    //2.增加下的title
    re.attributedTitle = [[NSAttributedString alloc]initWithString:@"你倒是用力拉啊"];
    //3.增加一个点击事件
    [re addTarget:self action:@selector(refreshAction:) forControlEvents:UIControlEventValueChanged];

    [self.tableView addSubview:re];

    //-----------搜索视图---------
    _searchView = [[UIView alloc]initWithFrame:CGRectMake(0, 30, 414, 42)];
    _searchView.backgroundColor = [UIColor clearColor];
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(10+10, 5, 30, 30)];
    [button setImage:[UIImage imageNamed:@"history.png"] forState:UIControlStateNormal];
    [_searchView addSubview:button];

    UIButton *collectionB = [[UIButton alloc]initWithFrame:CGRectMake(self.view.bounds.size.width-40, 5, 30, 30)];
    [collectionB setImage:[UIImage imageNamed:@"star.png"] forState:UIControlStateNormal];
    [collectionB setImage:[UIImage imageNamed:@"yellow_star.png"] forState:UIControlStateSelected];

    [_searchView addSubview:collectionB];

    //搜索框
    textF = [[UITextField alloc]initWithFrame:CGRectMake(10+10+30+10, 5, 270+30, 30)];
    textF.layer.borderWidth = 1;
    textF.layer.borderColor = [UIColor whiteColor].CGColor;
    textF.layer.cornerRadius = 5;
//    //2.富文本---带有属性的字符串
//    NSMutableAttributedString* maString =[[NSMutableAttributedString alloc]initWithString:@"🔍搜索目的地/攻略/景点/酒店等"];
//    //3.为属性字符串设置属性
//    //为属性字符串增加一个属性  属性名  属性值 范围值
//    [maString addAttribute:NSFontAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, 16)];
    textF.placeholder = @"🔍搜索目的地/攻略/景点/酒店等";
    textF.textColor = [UIColor whiteColor];
    textF.font = [UIFont systemFontOfSize:13];
    textF.textAlignment = NSTextAlignmentCenter;
    [_searchView addSubview:textF];

    [self.view addSubview:_searchView];

    //******************旅行直播********************
    oneImgV.image = [UIImage imageNamed:@"028b88bdb45fa0c1ad48882c4204501f.jpg"];
    oneLogo.layer.cornerRadius = 15;
    oneLogo.clipsToBounds = YES;
    oneLogo.image = [UIImage imageNamed:@"mafenwo_smile.jpg"];

    twoImgV.image = [UIImage imageNamed:@"1d0bd2fdf0dc51dc6c0c640d630f7cb7.jpg"];
    twoLogo.layer.cornerRadius = 15;
    twoLogo.clipsToBounds = YES;
    twoLogo.image = [UIImage imageNamed:@"mafenwo_smile.jpg"];

    threeImgV.image = [UIImage imageNamed:@"efca2cbeb81135bcc539733b390e21a1.jpg"];
    threeLogo.layer.cornerRadius = 15;
    threeLogo.clipsToBounds = YES;
    threeLogo.image = [UIImage imageNamed:@"mafenwo_smile.jpg"];

    _seeMoreButton.layer.borderWidth =1;
    _seeMoreButton.layer.borderColor = [UIColor orangeColor].CGColor;
    _seeMoreButton.layer.cornerRadius = 5;
    _seeMoreButton.clipsToBounds = YES;

    
    //********************自由行特惠******************
    freeSeeMore.layer.borderWidth =1;
    freeSeeMore.layer.borderColor = [UIColor orangeColor].CGColor;
    freeSeeMore.layer.cornerRadius = 5;
    freeSeeMore.clipsToBounds = YES;

    //自定义一些控件的属性
    [self customControl];

    //附近地图的滚动label
    [self addSCROllADView];
}

-(void)addSCROllADView{
    currentPage = 0;
    _scroV = [[UIScrollView alloc]initWithFrame:scrollADView.bounds];
    _scroV.contentSize = CGSizeMake(scrollADView.bounds.size.width, 5*scrollADView.bounds.size.height);
    _scroV.pagingEnabled = YES;
    //（5）实现代理方法
    _scroV.delegate = self;

    //循环添加imageView
    for (int i = 0; i<5; i++) {

//        UIImageView *imageV = 

        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, i*scrollADView.bounds.size.height, scrollADView.bounds.size.width, scrollADView.bounds.size.height)];

        button.tag = 2222+i;
        switch (i) {
            case 0:
                [button setTitle:@"我们的祖国" forState:UIControlStateNormal];
                [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];                button.titleLabel.textAlignment = NSTextAlignmentLeft;
                button.backgroundColor = [UIColor orangeColor];
                break;
            case 1:
                [button setTitle:@"是花园" forState:UIControlStateNormal];
                [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];                button.titleLabel.textAlignment = NSTextAlignmentLeft;
                button.backgroundColor = [UIColor greenColor];

                break;
            case 2:
                [button setTitle:@"花园的花朵" forState:UIControlStateNormal];
                [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                button.titleLabel.textAlignment = NSTextAlignmentLeft;
                button.backgroundColor = [UIColor orangeColor];
                break;
            case 3:
                [button setTitle:@"最讨厌" forState:UIControlStateNormal];
                [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                button.titleLabel.textAlignment = NSTextAlignmentLeft;
                button.backgroundColor = [UIColor greenColor];

                break;
            case 4:
                [button setTitle:@"年轻的白纸等着涂抹" forState:UIControlStateNormal];
                [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

//                button.titleLabel.text = @"德国总督府";
                button.titleLabel.textAlignment = NSTextAlignmentLeft;
                button.backgroundColor = [UIColor orangeColor];

                break;

            default:
                break;
        }

        [_scroV addSubview:button];
    }

    [scrollADView addSubview:_scroV];
    //添加定时器滚动广告
    [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
}

-(void)timerAction{

    if (currentPage == 4) {
        currentPage = 0;
        //新版本的动画方法，用于代替原来的
        [UIView animateWithDuration:.5 animations:^{
            _scroV.contentOffset = CGPointMake(0,currentPage*_scroV.bounds.size.height);
        }];

        return;
    }

    currentPage = currentPage+1;


    //新版本的动画方法，用于代替原来的
    [UIView animateWithDuration:.5 animations:^{
        _scroV.contentOffset = CGPointMake(0,currentPage*_scroV.bounds.size.height);
    }];
}


-(void)customControl{

    lookD.layer.cornerRadius = 5;
    lookD.clipsToBounds = YES;
    lookD.layer.borderWidth = 1;
    lookD.layer.borderColor = [UIColor orangeColor].CGColor;

    delicious.layer.cornerRadius = 5;
    delicious.clipsToBounds = YES;
    delicious.layer.borderWidth = 1;
    delicious.layer.borderColor = [UIColor orangeColor].CGColor;

    lookAllOnMap.layer.cornerRadius = 5;
    lookAllOnMap.clipsToBounds = YES;
    lookAllOnMap.layer.borderWidth = 1;
    lookAllOnMap.layer.borderColor = [UIColor orangeColor].CGColor;



}

-(void)awakeFromNib{
    //    self.view.backgroundColor = [UIColor greenColor];
    self.tabBarItem.image = [UIImage imageNamed:@"dasitana"];
}



//刷新Action
-(void)refreshAction:(UIRefreshControl*)re{
    //    re.refreshing  这是一个状态值，只读类型的

    //    if (re.refreshing == YES) {
    //        re.attributedTitle = [[NSAttributedString alloc]initWithString:@"正在刷新"];
    //    }

    //(1)开始刷新
    [re beginRefreshing];
    re.attributedTitle = [[NSAttributedString alloc]initWithString:@"满足了^_^ 刷新ing"];
    //结束刷新的方法
    //     [re endRefreshing];
    
    
    
}

//获取偏移量 停止刷新
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{

//    NSLog(@"targetContentOffset = %f",targetContentOffset->y);
    if (targetContentOffset->y >= 0.0) {
        //结束刷新的方法
        [re endRefreshing];
        re.attributedTitle = [[NSAttributedString alloc]initWithString:@"你倒是用力拉啊"];

        _offsetY = targetContentOffset->y;



    }
}
//
//-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
//
//////    _searchView = [[UIView alloc]initWithFrame:CGRectMake(0, 30, 414, 42)];
////    if (scrollView.contentOffset.y<=0) {
////        _searchView.frame = CGRectMake(0, 30, 414, 42);
////    }
////    if (scrollView.contentOffset.y>=210) {
////        _searchView.backgroundColor = [UIColor whiteColor];
////        textF.layer.borderColor = [UIColor orangeColor].CGColor;
////        textF.textColor = [UIColor grayColor];
////
////    }
////    if (scrollView.contentOffset.y<210) {
////        _searchView.backgroundColor = [UIColor clearColor];
////        textF.layer.borderColor = [UIColor whiteColor].CGColor;
////        textF.textColor = [UIColor whiteColor];
////
////    }
//
//
//
//
//}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{

    if (scrollView == self.view) {

        //    _searchView = [[UIView alloc]initWithFrame:CGRectMake(0, 30, 414, 42)];
        if (scrollView.contentOffset.y<=0) {
            _searchView.frame = CGRectMake(0, 30, 414, 42);
        }
        //    NSLog(@"%f",scrollView.contentOffset.y);
        if (scrollView.contentOffset.y>0) {
            _searchView.frame = CGRectMake(0,30+scrollView.contentOffset.y, 414, 42);
        }
        if (scrollView.contentOffset.y>=210) {
            _searchView.backgroundColor = [UIColor whiteColor];
            textF.layer.borderColor = [UIColor orangeColor].CGColor;
            textF.textColor = [UIColor grayColor];

        }
        if (scrollView.contentOffset.y<210) {
            _searchView.backgroundColor = [UIColor clearColor];
            textF.layer.borderColor = [UIColor whiteColor].CGColor;
            textF.textColor = [UIColor whiteColor];
            
        }

    }


}

- (IBAction)qindao2Action:(id)sender {
    //1.创建一个要切换的控制器
    PhotosViewController* photoVC = [[PhotosViewController alloc]init];
    UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:photoVC];
    [navi.navigationBar setBackgroundImage:[UIImage imageNamed:@"backg_navi.png"] forBarMetrics:UIBarMetricsDefault];
//    navi.navigationBar.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"backg_navi.png"]];
    //(2)模态视图
    //--------------模态视图---------
    //用于弹出临时的窗口

    //给twoC增加它的模态视图转场动画（很多种）
//    photoVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;

        [self presentViewController:navi animated:NO completion:^{


            //        NSLog(@"弹出页面以后会执行的代码");
        }];




}

//旅行直播
- (IBAction)seeMoreActionTwo:(id)sender {
    [self seeMoreAction:sender];
}
- (IBAction)seeMoreAction:(id)sender {
    TravelDirectSeedingVC *travelDirectSeedingVC = [[TravelDirectSeedingVC alloc]init];
    UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:travelDirectSeedingVC];
    
    [self presentViewController:navi animated:NO completion:^{

    }];
}
//自由行特惠
- (IBAction)freeSeeMoreActionTwo:(id)sender {
    [self freeSeeMoreAction:sender];
}

- (IBAction)freeSeeMoreAction:(id)sender {
    NSLog(@"自由行特惠");
}
#pragma -mark-----------------------城市地图---------------
//0.创建一个mapView和导入框架

//1.交给高德地图两个地标位置，自动帮我们实现导航
//只需要记住一个关键 MKMapItem

//2.需要CLPlacemark,地理编码
-(CLGeocoder*)geoC{
    //1.判断是否已经有了，若没有，则实行实例化
    if (!_geoC) {
        //实例化+代码
        _geoC = [[CLGeocoder alloc]init];
    }
    //返回值
    return _geoC;
}

- (IBAction)cityMapAction:(UIButton *)sender {
#pragma -mark 步骤三
    [self.geoC geocodeAddressString:@"北京" completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        CLPlacemark* starPK = [placemarks firstObject];

        [self.geoC geocodeAddressString:@"青岛" completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {

            CLPlacemark* endPK = [placemarks firstObject];


            [self getPlacemarkWithStar:starPK WithEnd:endPK];

        }];

    }];


}
-(void)getPlacemarkWithStar:(CLPlacemark*)starPK WithEnd:(CLPlacemark*)endPK{








#pragma -mark 步骤二
    //开始
    CLPlacemark* starClp = starPK;
    MKPlacemark* mkP = [[MKPlacemark alloc]initWithPlacemark:starClp];
    MKMapItem* starItem = [[MKMapItem alloc]initWithPlacemark:mkP];

    //结束
    CLPlacemark* endClp = endPK;
    MKPlacemark* mkP2 = [[MKPlacemark alloc]initWithPlacemark:endClp];
    MKMapItem* endItem = [[MKMapItem alloc]initWithPlacemark:mkP2];


#pragma -mark 步骤一
    //设置地图项数组
    NSArray* items = @[starItem,endItem];




    //设置启动字典
    NSDictionary* dic = @{

                          //设置显示交通
                          MKLaunchOptionsShowsTrafficKey :@(YES),

                          //设置地图的类型
                          MKLaunchOptionsMapTypeKey:@(MKMapTypeStandard),
                          // 导航方式
                          MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving
                          };



    //实现交给系统APP导航，只要记住一个关键字 MKMapItem
    [MKMapItem openMapsWithItems:items launchOptions:dic];


}
- (IBAction)lookDAction:(UIButton *)sender {
    NSLog(@"查看382各景点");
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
